﻿using Pay.Core;
using Pay.DTO;
using Pay.IService;
using Pay.TO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Pay.Web.Areas.Admin.Controllers
{
    [RouteArea("admin")]
    [RoutePrefix("playing")]
    public class PlayingController : AdminController
    {
        private IPlayingService _iPlayingService;
        private IPlayingTeamService _iPlayingTeamService;

        public PlayingController(IPlayingService iPlayingService, IPlayingTeamService iPlayingTeamService)
        {
            _iPlayingService = iPlayingService;
            _iPlayingTeamService = iPlayingTeamService;
        }

        #region 赛事管理
        public ActionResult Index()
        {
            return View();
        }

        public JsonResult Gets(PlayingQuery query)
        {
            var model = _iPlayingService.GetPlayings(query);
            return Json(ResultToJson.Json(new { data = model, e_playing_type_list = typeof(EPlayType).GetDropdownItemList() }), JsonRequestBehavior.AllowGet);
        }

        public JsonResult Get(long id)
        {
            var teamList = _iPlayingTeamService.GetTeamDropdownItems() ?? new List<DropdownItem>();

            playing_dto_ex model = new playing_dto_ex()
            {
                play_type = EPlayType.GroupCompetition,
                home_team_id = long.Parse(teamList.FirstOrDefault().Value),
                guest_team_id = long.Parse(teamList.FirstOrDefault().Value),
                play_time = DateTime.Now.AddDays(2),
                participation_end_time = DateTime.Now.AddDays(1)
            };
            if (id > 0)
                _iPlayingService.GetPlaying(id).ToCopyProperty(model);

            var winTeamList = (teamList.Where(o => o.Value == model.home_team_id.ToString() || o.Value == model.guest_team_id.ToString()).ToList() ?? new List<DropdownItem>());
            winTeamList.Add(new DropdownItem() { Value = "0", Text = "平局" });
            model.team_list = teamList;
            model.win_team_list = winTeamList;

            model.play_type_value = ((int)model.play_type).ToString();
            model.home_team_id_value = model.home_team_id.ToString();
            model.guest_team_id_value = model.guest_team_id.ToString();

            model.win_team_id_value = model.win_team_id != null ? model.win_team_id.ToString() : null;
            if (model.id > 0 && model.win_team_id == null)
            {
                model.win_team_id_value = winTeamList.FirstOrDefault().Value;
            }

            return Json(ResultToJson.Json(model), JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        [ValidateInput(false)]
        public JsonResult Add(playing_dto_ex dto)
        {
            dto.play_type = (EPlayType)(int.Parse(dto.play_type_value));
            dto.home_team_id = long.Parse(dto.home_team_id_value);
            dto.guest_team_id = long.Parse(dto.guest_team_id_value);
            var result = _iPlayingService.AddPlaying(dto);
            return Json(ResultToJson.Json(result > 0 ? " 保存成功" : "保存失败", result), JsonRequestBehavior.AllowGet);

        }

        [HttpPost]
        [ValidateInput(false)]
        public JsonResult Update(playing_dto_ex dto)
        {
            dto.play_type = (EPlayType)(int.Parse(dto.play_type_value));
            dto.home_team_id = long.Parse(dto.home_team_id_value);
            dto.guest_team_id = long.Parse(dto.guest_team_id_value);
            if (!string.IsNullOrEmpty(dto.win_team_id_value))
                dto.win_team_id = long.Parse(dto.win_team_id_value);
            var result = _iPlayingService.UpdatePlaying(dto);
            return Json(ResultToJson.Json(result > 0 ? " 保存成功" : "未做任何修改", result), JsonRequestBehavior.AllowGet);

        }
        #endregion


        #region 赛事参与统计
        public JsonResult GetPlayingStatistics(PageQuery query)
        {
            var model = _iPlayingService.GetPlayingStatistics(query);
            return Json(ResultToJson.Json(model), JsonRequestBehavior.AllowGet);
        }
        #endregion

        #region 后台赛事参与用户
        public JsonResult GetUserGuessPlayingHiss(UserGuessPlayingHisQuery query)
        {
            var model = _iPlayingService.GetUserGuessPlayingHiss(query);
            return Json(ResultToJson.Json(model), JsonRequestBehavior.AllowGet);
        }
        #endregion

    }
}